//
//  MPCompanyTaskController.m
//  MarkPlatform
//
//  Created by xiao6 on 2018/1/3.
//  Copyright © 2018年 AT. All rights reserved.
//

#import "MPCompanyTaskController.h"
#import "CompanyTaskCell.h"

@interface MPCompanyTaskController ()
{}
@property (nonatomic, strong) NSMutableArray<MPCompanyTaskModel *> *dataList;
@end

@implementation MPCompanyTaskController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"任务列表" backItem:YES];
    self.navigationItem.leftBarButtonItem = [self navItemWithImage:[UIImage imageNamed:@"nav_mine"] action:@selector(didClickLeftItem:)];
    [self setupRefresh:self.tableView option:ATRefreshDefault];
    [self setupEmpty:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(headerRefreshing) name:ATUserLoginNotification object:nil];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)refreshData:(NSInteger)page {
    if (![ATLocal user].isLogin) {
        [self endRefreshFailure];
        return;
    }
    [MPHttpTool POST:@"business/enterprise/queryMyTask"
              params:@{@"userid" : SafeDicObj([ATLocal user].userId),
                       @"page" : @(page),
                       @"rows" : @(RefreshPageSize),
                       }
             success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                 ATRefreshModel *model = [ATRefreshModel modelWithJSON:result.data];
                 if (page == RefreshPageStart) {
                     self.dataList = [NSMutableArray array];
                 }
                 [self.dataList addObjectsFromArray:[MPCompanyTaskModel modelArrayWithJson:model.rows]];
                 [self.tableView reloadData];
                 [self endRefresh:(model.total > self.dataList.count) && model.rows.count];
             } failure:ATDefaultFailure];
}

#pragma mark - actions
- (void)didClickLeftItem:(id)sender {
    [self.frostedViewController presentMenuViewController];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CompanyTaskCell *cell = [CompanyTaskCell cellForTableView:tableView indexPath:indexPath];
    cell.model = self.dataList[indexPath.row];
    @weakify(self)
    cell.didDelete = ^(CompanyTaskCell *cell) {
        @strongify(self)
        NSInteger index = [self.dataList indexOfObject:cell.model];
        if (index < self.dataList.count) {
            [self.dataList removeObjectAtIndex:index];
            [self.tableView deleteRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] withRowAnimation:UITableViewRowAnimationLeft];
        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
